import constant from './constant'

// 存储变量名
let storageKey = 'storage_data'

// 存储节点变量名
let storageNodeKeys = [constant.avatar, constant.name, constant.user, constant.roles, constant.permissions]

// 存储的数据
let storageData = uni.getStorageSync(storageKey) || {}

const storage = {
	set: function(key, value) {
		if (storageNodeKeys.indexOf(key) != -1) {
			let tmp = uni.getStorageSync(storageKey)
			tmp = tmp ? tmp : {}
			tmp[key] = value
			uni.setStorageSync(storageKey, tmp)
		} else {
			console.log('storage.set', key, value)
			uni.setStorageSync(key, value) //非登出需要清理的缓存
		}
	},
	get: function(key) {
		if (storageNodeKeys.indexOf(key) != -1) {
			return storageData[key] || ""
		} else {
			console.log('storage.get', key, uni.getStorageSync(key),storageNodeKeys)
			return uni.getStorageSync(key) //非登出需要清理的缓存
		}
	},
	remove: function(key) {
		delete storageData[key]
		uni.setStorageSync(storageKey, storageData)
	},
	clean: function() {
		uni.removeStorageSync(storageKey)
	}
}

export default storage